package com.kld.yqyj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.kld.common.utils.StringUtils;
import com.kld.common.core.page.TableDataInfo;
import com.kld.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kld.yqyj.domain.bo.TInjwwDBo;
import com.kld.yqyj.domain.vo.TInjwwDVo;
import com.kld.yqyj.domain.TInjwwD;
import com.kld.yqyj.mapper.TInjwwDMapper;
import com.kld.yqyj.service.ITInjwwDService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 注水井生产日报Service业务层处理
 *
 * @author bw
 * @date 2024-05-15
 */
@RequiredArgsConstructor
@Service
public class TInjwwDServiceImpl implements ITInjwwDService {

    private final TInjwwDMapper baseMapper;

    /**
     * 查询注水井生产日报
     *
     * @param wellId 注水井生产日报主键
     * @return 注水井生产日报
     */
    @Override
    public TInjwwDVo queryById(String wellId){
        return baseMapper.selectVoById(wellId);
    }

    /**
     * 查询注水井生产日报列表
     *
     * @param bo 注水井生产日报
     * @return 注水井生产日报
     */
    @Override
    public TableDataInfo<TInjwwDVo> queryPageList(TInjwwDBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TInjwwD> lqw = buildQueryWrapper(bo);
        Page<TInjwwDVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询注水井生产日报列表
     *
     * @param bo 注水井生产日报
     * @return 注水井生产日报
     */
    @Override
    public List<TInjwwDVo> queryList(TInjwwDBo bo) {
        LambdaQueryWrapper<TInjwwD> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TInjwwD> buildQueryWrapper(TInjwwDBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TInjwwD> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getWellName()), TInjwwD::getWellName, bo.getWellName());
        lqw.eq(StringUtils.isNotBlank(bo.getCW()), TInjwwD::getCW, bo.getCW());
        lqw.eq(StringUtils.isNotBlank(bo.getZsfs()), TInjwwD::getZsfs, bo.getZsfs());
        lqw.eq(bo.getProdTime() != null, TInjwwD::getProdTime, bo.getProdTime());
        lqw.eq(bo.getTubingPre() != null, TInjwwD::getTubingPre, bo.getTubingPre());
        lqw.eq(bo.getCasingPre() != null, TInjwwD::getCasingPre, bo.getCasingPre());
        lqw.eq(bo.getDInjW() != null, TInjwwD::getDInjW, bo.getDInjW());
        lqw.eq(bo.getDInjWPlan() != null, TInjwwD::getDInjWPlan, bo.getDInjWPlan());
        lqw.eq(StringUtils.isNotBlank(bo.getREMARK()), TInjwwD::getREMARK, bo.getREMARK());
        return lqw;
    }

    /**
     * 新增注水井生产日报
     *
     * @param bo 注水井生产日报
     * @return 结果
     */
    @Override
    public Boolean insertByBo(TInjwwDBo bo) {
        TInjwwD add = BeanUtil.toBean(bo, TInjwwD.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setWellId(add.getWellId());
        }
        return flag;
    }

    /**
     * 修改注水井生产日报
     *
     * @param bo 注水井生产日报
     * @return 结果
     */
    @Override
    public Boolean updateByBo(TInjwwDBo bo) {
        TInjwwD update = BeanUtil.toBean(bo, TInjwwD.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TInjwwD entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除注水井生产日报
     *
     * @param wellIds 需要删除的注水井生产日报主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
